package com.hinnotek.amoeba.ui.activity.old

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.codbking.calendar.CalendarDate
import com.codbking.calendar.CalendarView.OnTodaySelectStatusChangedListener
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.databinding.ActivityMaterialDesignCalendarBinding
import com.xuexiang.xui.utils.DensityUtils
import com.xuexiang.xui.utils.ThemeUtils
import java.util.*

open class MaterialDesignCalendarActivity : BaseActivity<ActivityMaterialDesignCalendarBinding>(),
    View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_material_design_calendar)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.iv_previous -> bindingView.calendarDateView.previous()
            R.id.iv_next -> bindingView.calendarDateView.next()
            else -> {
            }
        }
    }

    override fun initVew() {
        bindingView.ivPrevious.setOnClickListener(this)
        bindingView.ivNext.setOnClickListener(this)
        bindingView.calendarDateView.setAdapter { convertView: View?, parentView: ViewGroup, calendarDate: CalendarDate ->
            var inConvertView = convertView
            if (inConvertView == null) {
                inConvertView = LayoutInflater.from(parentView.context)
                    .inflate(R.layout.adapter_calendar_item, null)
                val params =
                    ViewGroup.LayoutParams(DensityUtils.dp2px(48f), DensityUtils.dp2px(48f))
                inConvertView?.layoutParams = params
            }
            val textView: TextView = inConvertView!!.findViewById(R.id.tv_text)
            textView.setBackgroundResource(R.drawable.bg_calendar_material_design_item)
            textView.text = calendarDate.day.toString()
            if (calendarDate.monthFlag != 0) {
                textView.setTextColor(-0x6d665f)
            } else {
                if (calendarDate.isToday && calendarDate.equals(bindingView.calendarDateView.getSelectCalendarDate())) {
                    textView.setTextColor(ThemeUtils.resolveColor(mContext, R.attr.colorAccent))
                } else {
                    textView.setTextColor(
                        ThemeUtils.resolveColor(mContext, R.attr.xui_config_color_content_text)
                    )
                }
            }
            inConvertView
        }
        bindingView.calendarDateView.setOnCalendarSelectedListener { view: View?, _: Int, calendarDate: CalendarDate ->
            val intent = Intent()
            intent.putExtra("date", calendarDate.formatDate())
            setResult(RESULT_OK, intent)
            finish()
        }
        bindingView.calendarDateView.setOnTodaySelectStatusChangedListener(
            OnTodaySelectStatusChangedListener { todayView: View, isSelected: Boolean ->
                val view = todayView.findViewById<TextView>(R.id.tv_text)
                if (isSelected) {
                    view.setTextColor(
                        ThemeUtils.resolveColor(mContext, R.attr.xui_config_color_content_text)
                    )
                } else {
                    view.setTextColor(ThemeUtils.resolveColor(mContext, R.attr.colorAccent))
                }
            })
        bindingView.calendarDateView.setOnMonthChangedListener { view: View?, position: Int, date: CalendarDate ->
            bindingView.tvTitle.text = String.format("%d年%d月", date.year, date.month)
        }
        val date = CalendarDate.get(Date())
        bindingView.tvTitle.text = String.format("%d年%d月", date.year, date.month)

    }

    override fun setData() {}
}